﻿using System;
using System.Collections.Generic;
using System.Text;
using ZhCun.Framework.DataAccess.DbHelper;
using ZhCun.Framework.DataAccess.BuildSQLText;
using ZhCun.Framework.Common.Exceptions;
using System.Data.Common;
using System.Data.SQLite;
using ZhCun.Framework.Common.Models.WhereHelperModel;
using ZhCun.Framework.DataAccess.WhereHelperRealize;

namespace ZhCun.Framework.DataAccess.DAL
{
    public class DalBaseSQLite : DalBase
    {
        #region 构造函数

        public DalBaseSQLite()
        { }
        public DalBaseSQLite(string connStr)
        {
            _ConnectString = connStr;
        }

        #endregion

        #region 变量

        DbHelperBase _DBHelper;
        BuildSQL _BuildSQLTextObj;        
        string _ConnectString;

        #endregion

        #region 父类重写

        public override DbHelperBase DBHelper
        {
            get
            {
                if (_DBHelper == null)
                {
                    //if (string.IsNullOrEmpty(_ConnectString))
                    //{
                    //    ConfigFactory.GetMainConnStr();
                    //}
                    if (string.IsNullOrEmpty(_ConnectString))
                    {
                        string errMsg = string.Format("在创建Dal对象时,未正确赋值连接字符串!");
                        throw new FrameworkException(errMsg);
                    }
                    _DBHelper = new SQLiteHelper(_ConnectString);
                }
                return _DBHelper;
            }
        }

        protected internal override BuildSQL BuildSQLTextObj
        {
            get
            {
                if (_BuildSQLTextObj == null)
                {
                    _BuildSQLTextObj = new BuildSQL_SQLite();
                }
                return _BuildSQLTextObj;
            }
        }

        protected internal override DbParameter GetDbParam(string paramName, object paramValue)
        {
            return new SQLiteParameter(paramName, paramValue);
        }

        #endregion

        public override DateTime GetNowTime()
        {
            object nowTimeObj = DBHelper.GetScalar("select datetime('now', 'localtime')");
            return Convert.ToDateTime(nowTimeObj);
        }

        internal override WhereHelper CreateWhereHelper()
        {
            return new WhereHelperSQLite();
        }
    }
}
